package com.bsoft.gol.hcs.specialist.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import com.bsoft.gol.hcs.enumcase.FollowTypeEnum;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import com.bsoft.gol.hcs.specialist.bean.response.FactorData;
import com.bsoft.gol.hcs.specialist.entity.FusAnswerDiagnosisInfo;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author tongtp
 * @version v0.1
 * @className FusAnswerDiagnosisInfoDAO
 * @description
 * @create 2020-07-31 15:07
 **/
public abstract class FusAnswerDiagnosisInfoDAO extends HibernateSupportDelegateDAO<FusAnswerDiagnosisInfo> {

	/**
	 * @param reportId
	 * @throws
	 * @description: 根据表单答案唯一记录id查询
	 * @return: specialist.entity.FusAnswerDiagnosisInfo
	 * @author: tongtp
	 * @date: 2020/8/4 17:57
	 */
	@DAOMethod(sql = " from FusAnswerDiagnosisInfo where reportId = :reportId AND diagnosisId = :diagnosisId")
	public abstract FusAnswerDiagnosisInfo getByReportIdAndDiagnosisId(@DAOParam("reportId") String reportId,
																	   @DAOParam("diagnosisId") String diagnosisId);

	/**
	 * 根据用户和病种查询ReportId
	 *
	 * @param diseaseTypeId
	 * @param patientId
	 * @param patientMedicalCardType
	 * @param patientMedicalCardNumber
	 * @param cardType
	 * @param cardNumber
	 * @return java.util.List<java.lang.String>
	 * @throws
	 * @author tongtp
	 * @date 2021/12/29 13:41
	 */
	public List<String> queryReportId(Long diseaseTypeId, String patientId,
									  Integer patientMedicalCardType, String patientMedicalCardNumber,
									  String cardType, String cardNumber) {
		HibernateStatelessResultAction<List<String>> action =
				new AbstractHibernateStatelessResultAction<List<String>>() {
					@Override
					public void execute(StatelessSession ss) throws Exception {
						StringBuilder sqlBuilder = new StringBuilder();
						sqlBuilder.append("select report_id from fus_answer_diagnosis_info" +
								"  where disease_type_id = :diseaseTypeId ");
						sqlBuilder.append(" and ( ");
						if (CommonUtils.isNotNull(patientId)) {
							sqlBuilder.append(" patient_id = :patientId ");
						}
						if (CommonUtils.isNotNull(patientMedicalCardType) && CommonUtils.isNotNull(patientMedicalCardNumber)) {
							sqlBuilder.append("  or (patient_medical_card_type = :patientMedicalCardType and" +
									" patient_medical_card_number = :patientMedicalCardNumber) ");
						}
						if (CommonUtils.isNotNull(cardType) && CommonUtils.isNotNull(cardNumber)) {
							sqlBuilder.append("  or (card_type = :cardType and" +
									" card_number = :cardNumber) ");
						}
						sqlBuilder.append(" ) ");
						Query query = ss.createSQLQuery(sqlBuilder.toString());
						query.setParameter("diseaseTypeId", diseaseTypeId);
						if (CommonUtils.isNotNull(patientId)) {
							query.setParameter("patientId", patientId);
						}
						if (CommonUtils.isNotNull(patientMedicalCardType) && CommonUtils.isNotNull(patientMedicalCardNumber)) {
							query.setParameter("patientMedicalCardType", patientMedicalCardType);
							query.setParameter("patientMedicalCardNumber", patientMedicalCardNumber);
						}
						if (CommonUtils.isNotNull(cardType) && CommonUtils.isNotNull(cardNumber)) {
							query.setParameter("cardType", cardType);
							query.setParameter("cardNumber", cardNumber);
						}
						List list = query.list();
						List<String> voList;
						if (CommonUtils.isNotEmpty(list)) {
							voList = list;
						} else {
							voList = new ArrayList<>();
						}
						setResult(voList);
					}
				};
		HibernateSessionTemplate.instance().execute(action);
		return action.getResult();
	}

	public List<FactorData> executeFactorForDiagnosis(List<String> factorIdList, Date startTime, Date endTime, String diagnosisServiceType, List<String> patientIdList,Byte isLoopExecute,Long routeDetailId,String followType) {
		HibernateStatelessResultAction<List<FactorData>> action = new AbstractHibernateStatelessResultAction<List<FactorData>>() {
			@Override
			public void execute(StatelessSession ss) throws Exception {
				StringBuilder sqlBuilder = new StringBuilder();
				sqlBuilder.append("select patientId,diagnosticRecordId,diagnosisServiceType,diseasesCode ," +
						"diseasesName,diagnosisTime,dataSource from (");
				sqlBuilder.append("SELECT patient_id as patientId,report_id diagnosticRecordId,diagnosis_type " +
						"diagnosisServiceType, ");
				sqlBuilder.append(" diagnosis_id diseasesCode,diagnosis_name diseasesName,answer_time diagnosisTime," +
						"'2' dataSource");
				sqlBuilder.append("  FROM `fus_answer_diagnosis_info`  ");
				sqlBuilder.append(" WHERE diagnosis_id in(:diseasesCodeList) and " +
						"diagnosis_type=:diagnosisServiceType ");
				if (null != startTime) {
					sqlBuilder.append("  and  answer_time>:startTime ");
				}
				if (null != endTime) {
					sqlBuilder.append("  and  answer_time<:endTime ");
				}
				if (CommonUtils.isNotEmpty(patientIdList)) {
					sqlBuilder.append(" and patient_id in(:patientIdList)");
				}
				sqlBuilder.append(") AS  m ");
				//循环执行机制 0 不执行 1 执行
				if (FollowTypeEnum.FUS_SPECIALIST.getType().equals(followType) && null != isLoopExecute && isLoopExecute == 1) {
					sqlBuilder.append(" LEFT JOIN fus_factor_data_record f  ON f.patient_id = m.patientId   AND f.route_detail_id = :routeDetailId AND f.diagnostic_record_id = m.diagnosticRecordId WHERE f.id IS NULL  ");
				}
				Query query = ss.createSQLQuery(sqlBuilder.toString());
				query.setParameterList("diseasesCodeList", factorIdList);
				query.setParameter("diagnosisServiceType", diagnosisServiceType);
				if (null != startTime) {
					query.setParameter("startTime", startTime);
				}
				if (null != endTime) {
					query.setParameter("endTime", endTime);
				}

				if (CommonUtils.isNotEmpty(patientIdList)) {
					query.setParameterList("patientIdList", patientIdList);
				}
				if (FollowTypeEnum.FUS_SPECIALIST.getType().equals(followType) && null != isLoopExecute && isLoopExecute == 1) {
					query.setParameter("routeDetailId", routeDetailId);
				}
				query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
				List<Map> list = query.list();
				List<FactorData> voList = new ArrayList<>();
				if (CommonUtils.isNotEmpty(list)) {
					for (Map map : list) {
						voList.add(ConversionUtils.convert(map, FactorData.class));
					}
				} else {
					voList = new ArrayList<>();
				}
				setResult(voList);
			}
		};
		HibernateSessionTemplate.instance().execute(action);
		return action.getResult();
	}
}
